package com.ltkj.shop.service.impl;

import java.util.List;

import com.ltkj.common.core.context.SecurityContextHolder;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.shop.mapper.ShopReceiveAddressBizMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.shop.mapper.ShopReceiveAddressMapper;
import com.ltkj.shop.domain.ShopReceiveAddress;
import com.ltkj.shop.service.IShopReceiveAddressService;

/**
 * 收货地址Service业务层处理
 * 
 * @author xupf
 * @date 2023-08-29
 */
@Service
public class ShopReceiveAddressServiceImpl implements IShopReceiveAddressService 
{
    @Autowired
    private ShopReceiveAddressMapper shopReceiveAddressMapper;
    @Autowired
    private ShopReceiveAddressBizMapper shopReceiveAddressBizMapper;

    /**
     * 查询收货地址
     * 
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public ShopReceiveAddress selectShopReceiveAddressById(Long id)
    {
        return shopReceiveAddressMapper.selectShopReceiveAddressById(id);
    }

    /**
     * 查询收货地址列表
     * 
     * @param shopReceiveAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<ShopReceiveAddress> selectShopReceiveAddressList(ShopReceiveAddress shopReceiveAddress)
    {
        return shopReceiveAddressMapper.selectShopReceiveAddressList(shopReceiveAddress);
    }

    /**
     * 新增收货地址
     * 
     * @param shopReceiveAddress 收货地址
     * @return 结果
     */
    @Override
    public int insertShopReceiveAddress(ShopReceiveAddress shopReceiveAddress)
    {
        shopReceiveAddress.setCreateTime(DateUtils.getNowDate());
        shopReceiveAddress.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        shopReceiveAddress.setUserId(SecurityUtils.getLoginUser().getUserid());
        if ("1".equals(shopReceiveAddress.getIsDefault())) {
            setDefaultReceiveAddress(shopReceiveAddress.getUserId(), shopReceiveAddress.getId());
        }
        return shopReceiveAddressMapper.insertShopReceiveAddress(shopReceiveAddress);
    }

    /**
     * 修改收货地址
     * 
     * @param shopReceiveAddress 收货地址
     * @return 结果
     */
    @Override
    public int updateShopReceiveAddress(ShopReceiveAddress shopReceiveAddress)
    {
        shopReceiveAddress.setUpdateTime(DateUtils.getNowDate());
        shopReceiveAddress.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        if ("1".equals(shopReceiveAddress.getIsDefault())) {
            setDefaultReceiveAddress(shopReceiveAddress.getUserId(), shopReceiveAddress.getId());
        }
        return shopReceiveAddressMapper.updateShopReceiveAddress(shopReceiveAddress);
    }

    /**
     * 设置其余地址非默认
     * @param userId
     * @param id
     * @return
     */
    public int setDefaultReceiveAddress(Long userId, Long id)
    {
        return shopReceiveAddressBizMapper.setDefault(userId, id);
    }

    /**
     * 批量删除收货地址
     * 
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteShopReceiveAddressByIds(Long[] ids)
    {
        return shopReceiveAddressMapper.deleteShopReceiveAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     * 
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteShopReceiveAddressById(Long id)
    {
        return shopReceiveAddressMapper.deleteShopReceiveAddressById(id);
    }
}
